package com.lovi.homework;

/**
 * 计算字符串中字母、数字、空格和其他字符的个数
 */
public class CalChar {
    public static void main(String[] args) {
        int lowerCount = 0;
        int upperCount = 0;
        int numCount = 0;
        int spaceCount = 0;
        int otherCount = 0;
        String str = "I am a student.I am from China.I study in a school. 19 20 21";
        char[] chars = str.toCharArray();
        for (char c : chars) {
            //大写字母
            if (c >= 'A' && c <= 'Z') {
                upperCount++;
            }
            //小写字母
            else if (c >= 'a' && c <= 'z') {
                lowerCount++;
            }
            //数字
            else if (c >= '0' && c <= '9') {
                numCount++;
            }
            //空格
            else if (c == ' ') {
                spaceCount++;
            }
            //其他字符
            else {
                otherCount++;
            }


        }
        System.out.println("大写字母个数：" + upperCount +
                "\n小写字母个数：" + lowerCount + "\n空格个数：" + spaceCount +
                "\n数字个数：" + numCount + "\n其他字符个数：" + otherCount);

    }
}
